package com.neuedu.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.neuedu.entity.Food;
import com.neuedu.service.FoodService;
import com.neuedu.vo.ResultJson;
import io.minio.errors.*;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * <p>
 * 食品表 前端控制器
 * </p>
 *
 * @author wangyu
 * @since 2025-10-21
 */
@RestController
@RequestMapping("/food")
public class FoodController {
    @Resource
    FoodService foodService;

    @GetMapping("/list")
    ResultJson<IPage<Food>> list(Integer pageNo, Integer pageSize, String name, String type) {
        return ResultJson.success(foodService.list(pageNo, pageSize, name, type));
    }
    @PostMapping("/add")
    ResultJson<Boolean> add(Food food, MultipartFile file) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        return ResultJson.success(foodService.add(food, file), "添加食品成功");
    }
    @PostMapping("/delete")
    ResultJson<Boolean> delete(Integer id) {
        return ResultJson.success(foodService.removeById(id), "删除食品成功");
    }
    @GetMapping("/getById")
    ResultJson<Food> getById(Integer id) {
        return ResultJson.success(foodService.getById(id));
    }
    @PostMapping("/update")
    ResultJson<Boolean> update(Food food, MultipartFile file) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        return ResultJson.success(foodService.update(food, file), "修改食品成功");
    }
    @GetMapping("/getList")
    ResultJson<List<Food>> getList() {
        return ResultJson.success(foodService.getList());
    }
}
